<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 2017/9/5
 * Time: 11:05
 */

/**
 * Created by wanglu on 2017/9/5.
 */
class Key_service extends MY_Service
{


    public function __construct()
    {
        parent::__construct();
        $this->load->model('key_model');
    }


    public function import_add_key($pid, $key, $display_order)
    {

        $keyextis = $this->key_model->getRecord(array('pkey' => $key, 'pid'=> $pid));
        //只允许唯一
        if (empty($keyextis)) {
            $keyData = array(
                'pid'=> $pid,
                'pkey'=> $key,
                'limit_length'=> -1,
                'remark'=> '',
                'createtime' => time(),
                'display_order' => $display_order
            );
           return  $this->key_model->createRecord($keyData);
        }
    }

    /**
     * 更新排序
     * @param $pid
     * @param array $key_sorts
     * @return mixed
     */
    public function update_key_sort($pid, $key_sorts = array() ) {
        return $this->key_model->batch_update_order($pid, $key_sorts);
    }

    public function get_key_by_id($kid) {
        return $this->key_model->getRecord(array('id'=> $kid));
    }

    /**
     * 更新KEY
     * @param $pid
     * @param $id
     * @param $pkey
     * @param $limit_length
     * @param $remark
     * @return mixed
     * @throws AppException
     */
    public function update_key($pid, $id, $pkey, $limit_length, $remark) {
        $key_exists  = $this->key_model->getRecord(array('id' => $id));
        if (empty($key_exists)) {
            throw  new AppException(ErrorCode::FAIL, " key is not exits!");
        }

        //修改KEY
        if ($key_exists['pkey'] != $pkey) {

            //查询是否有同名存在
            $pkey_exists = $this->key_model->getRecord(array('pkey' => $pkey, 'pid'=> $pid));

            if (!empty($pkey_exists)) {
                throw  new AppException(ErrorCode::FAIL, " key  is exits,  update fail, please change user key!");
            }
        }

        $key_data  = array(
            'pkey'=> $pkey,
            'limit_length' => $limit_length,
            'remark' => $remark
        );

        return $this->key_model->updateRecord($key_data,  array('id'=> $id));
    }


    public function add_key($pid, $pkey, $limit_length, $remark, $display_order) {

        $keyextis = $this->key_model->getRecord(array('pkey' => $pkey, 'pid'=> $pid));

        if (!empty($keyextis)) {
            throw  new AppException(ErrorCode::FAIL, " pkey is exits add faill!");
        }

        $key = array(
            'pid'=> $pid,
            'pkey'=> $pkey,
            'limit_length' => $limit_length,
            'remark' => $remark,
            'display_order' => $display_order,
            'createtime' => time()
        );

        return $this->key_model->createRecordReturnId($key);
    }


    public function update_key_key($pid, $id, $pkey) {
        $key_exists  = $this->key_model->getRecord(array('id' => $id));
        if (empty($key_exists)) {
            throw  new AppException(ErrorCode::FAIL, " key is not exits!");
        }

        //修改KEY
        if ($key_exists['pkey'] != $pkey) {

            //查询是否有同名存在
            $pkey_exists = $this->key_model->getRecord(array('pkey' => $pkey, 'pid'=> $pid));

            if (!empty($pkey_exists)) {
                throw  new AppException(ErrorCode::FAIL, " key  is exits,  update fail, please change user key!");
            }
        }

        $key_data  = array(
            'pkey'=> $pkey
        );

        return $this->key_model->updateRecord($key_data,  array('id'=> $id));
    }


}
